import { ActionFn } from './type'
import { PLAY_MODE } from '@/assets/js/constant'
import { shuffle } from '@/assets/js/util'

// 顺序播放
export const selectSong: ActionFn = ({ commit, state }, { list, index }) => {
    commit('setSequenceList', list)
    commit('setPlayingState', true)
    commit('setFullScreen', true)
    commit('setPlayList', list)
    commit('setCurrentIndex', index)
    commit('setPlayMode', PLAY_MODE.sequence)
}

// 随机播放
export const shuffleSong: ActionFn = ({ commit }, list) => {
    commit('setSequenceList', list)
    commit('setPlayingState', true)
    commit('setFullScreen', true)
    commit('setPlayList', shuffle(list))
    commit('setCurrentIndex', 0)
    commit('setPlayMode', PLAY_MODE.random)
}

// 改变mode播放状态
export const changeMode: ActionFn = ({ commit, state, getters }, mode) => {
    const id = getters.playingSong.id // 修改之前，先保存正在播放歌曲的id
    if (mode === PLAY_MODE.sequence) { // 顺序播放
        commit('setPlayList', state.sequenceList)
    } else if (mode === PLAY_MODE.random) { // 随机播放
        commit('setPlayList', shuffle(state.sequenceList))
    }
    // 走到这里，playList已经更新为对应的播放列表了，在playList中找到原来歌曲id的索引，并重新重新重新更改currentIndex
    const newIndex = state.playList.findIndex(one => one.id === id)
    commit('setCurrentIndex', newIndex)
    commit('setPlayMode', mode)
}
